package com.he.controller;

import com.he.domain.Result;
import com.he.domain.dto.*;
import com.he.domain.vo.TaskBackVO;
import com.he.service.TaskService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author He
 * @version 1.0
 * @Date 2023/10/20 16:11
 * @Desc 定时任务模块
 */
@Tag(name = "定时任务模块")
@RestController
public class TaskController {

    @Resource
    private TaskService taskService;

    /**
     * 查看定时任务列表
     *
     * @param condition 条件
     * @return {@link TaskBackVO} 定时任务列表
     */
    @Operation(description = "查看定时任务列表")
    @PreAuthorize("@ps.hasPermission('monitor:task:list')")
    @GetMapping("/admin/task/list")
    public Result<PageResult<TaskBackVO>> listTaskBackVO(ConditionDTO condition) {
        return Result.success(taskService.listTaskBackVO(condition));
    }

    /**
     * 添加定时任务
     *
     * @param task 定时任务信息
     * @return {@link Result<>}
     */
    @Operation(description = "添加定时任务")
    @PreAuthorize("@ps.hasPermission('monitor:task:add')")
    @PostMapping("/admin/task/add")
    public Result<?> addTask(@Validated @RequestBody TaskDTO task) {
        taskService.addTask(task);
        return Result.success();
    }

    /**
     * 修改定时任务
     *
     * @param task 定时任务信息
     * @return {@link Result<>}
     */
    @Operation(description = "修改定时任务")
    @PreAuthorize("@ps.hasPermission('monitor:task:update')")
    @PutMapping("/admin/task/update")
    public Result<?> updateTask(@Validated @RequestBody TaskDTO task) {
        taskService.updateTask(task);
        return Result.success();
    }

    /**
     * 删除定时任务
     *
     * @param taskIdList 定时任务id集合
     * @return {@link Result<>}
     */
    @Operation(description = "删除定时任务")
    @PreAuthorize("@ps.hasPermission('monitor:task:delete')")
    @DeleteMapping("/admin/task/delete")
    public Result<?> deleteTask(@RequestBody List<Integer> taskIdList) {
        taskService.deleteTask(taskIdList);
        return Result.success();
    }

    /**
     * 修改定时任务状态
     *
     * @param taskStatus 定时任务状态
     * @return {@link Result<>}
     */
    @Operation(description = "修改定时任务状态")
    @PreAuthorize("@ps.hasAllPermissions({'monitor:task:status,monitor:task:update'})")
    @PutMapping("/admin/task/changeStatus")
    public Result<?> updateTaskStatus(@Validated @RequestBody StatusDTO taskStatus) {
        taskService.updateTaskStatus(taskStatus);
        return Result.success();
    }

    /**
     * 执行定时任务
     *
     * @param taskRun 运行定时任务
     * @return {@link Result<>}
     */
    @Operation(description = "执行定时任务")
    @PreAuthorize("@ps.hasPermission('monitor:task:run')")
    @PutMapping("/admin/task/run")
    public Result<?> runTask(@RequestBody TaskRunDTO taskRun) {
        taskService.runTask(taskRun);
        return Result.success();
    }

}

